package com.wgworkshop.pems.service;

import com.wgworkshop.pems.common.BusinessException;
import com.wgworkshop.pems.common.CommonUtils;
import com.wgworkshop.pems.entity.HolidayEntity;
import com.wgworkshop.pems.model.HolidayModel;
import com.wgworkshop.pems.repository.HolidayRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 假期服务接口实现类。
 * Created by wlc on 2017/7/9.
 */
@Service
public class HolidayServiceImpl implements HolidayService {
    @Autowired
    private HolidayRepository holidayRepository;

    @Override
    public HolidayModel save(HolidayModel holidayModel) {
        HolidayEntity holidayEntity = new HolidayEntity();
        BeanUtils.copyProperties(holidayModel, holidayEntity);
        HolidayEntity resultEntity = holidayRepository.save(holidayEntity);
        HolidayModel resultModel = new HolidayModel();
        BeanUtils.copyProperties(resultEntity, resultModel);
        return resultModel;
    }

    @Override
    public void delete(Long id) {
        checkExists(id);
        holidayRepository.delete(id);
    }

    @Override
    public HolidayModel update(Long id, HolidayModel holidayModel) {
        checkExists(id);

        HolidayEntity holidayEntity = holidayRepository.findOne(id);
        String[] nullProperties = CommonUtils.getNullPropertyNames(holidayModel);
        BeanUtils.copyProperties(holidayModel, holidayEntity, nullProperties);

        HolidayEntity resultEntity = holidayRepository.save(holidayEntity);
        HolidayModel resultModel = new HolidayModel();
        BeanUtils.copyProperties(resultEntity, resultModel);

        return resultModel;
    }

    @Override
    public HolidayModel update(HolidayModel holidayModel) {
        Long holidayId = holidayModel.getId();
        checkExists(holidayId);

        HolidayEntity holidayEntity = holidayRepository.findOne(holidayId);
        BeanUtils.copyProperties(holidayModel, holidayEntity);

        HolidayEntity resultEntity = holidayRepository.save(holidayEntity);
        HolidayModel resultModel = new HolidayModel();
        BeanUtils.copyProperties(resultEntity, resultModel);

        return resultModel;
    }

    @Override
    public HolidayModel findOne(Long id) {
        HolidayEntity holidayEntity = holidayRepository.findOne(id);
        HolidayModel holidayModel = null;
        if (holidayEntity != null) {
            holidayModel = new HolidayModel();
            BeanUtils.copyProperties(holidayEntity, holidayModel);
        }

        return holidayModel;
    }

    @Override
    public List<HolidayModel> findAll() {
        Iterable<HolidayEntity> holidayEntities = holidayRepository.findAll();
        return CommonUtils.convertIterableToList(holidayEntities, HolidayModel.class);
    }

    @Override
    public List<HolidayModel> findByNameContaining(String name) {
        String searchName = name != null ? name : "";
        Iterable<HolidayEntity> holidayEntities = holidayRepository.findByNameContaining(searchName);
        return CommonUtils.convertIterableToList(holidayEntities, HolidayModel.class);
    }

    @Override
    public boolean exists(Long id) {
        return holidayRepository.exists(id);
    }

    @Override
    public void checkExists(Long id) {
        if (!exists(id)) {
            throw new BusinessException(String.format("指定的假期ID不存在[%s]。", id));
        }
    }
}
